package com.hunlian.makelove.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.bean.ChangePwBean;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.ChangePwServiceVisit;
import com.hunlian.makelove.tools.MD5Tools;
import com.hunlian.makelove.tools.MyToast;
import com.hunlian.makelove.view.ClearEditText;


public class ChangePasswordActivity extends BaseActivity {


    private ChangePasswordActivity mActivity;
    private String mTitle;
    private ClearEditText mCETCurPw;
    private ClearEditText mCETNewPw;
    private ClearEditText mCETVerifyPw;
    private ChangePwBean mChangePwBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        mActivity = this;
        mTitle = "修改密码";

        RelativeLayout ll_title = (RelativeLayout) this.findViewById(R.id.ll_title);
        TextView mTvTitle = (TextView) ll_title.findViewById(R.id.title);
        mTvTitle.setText(mTitle);
        Button btnR = (Button) ll_title.findViewById(R.id.btn_nav_right);
        btnR.setText("保存");
        btnR.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                save();
            }

        });

        ImageButton mBtnBack = (ImageButton) ll_title.findViewById(R.id.back);
        mBtnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mActivity.finish();
            }

        });

        mCETCurPw = (ClearEditText) this.findViewById(R.id.cet_cur_pw);
        mCETNewPw = (ClearEditText) this.findViewById(R.id.cet_new_pw);
        mCETVerifyPw = (ClearEditText) this.findViewById(R.id.cet_verify_pw);

    }

    private boolean validate() {

        // 非空验证
        if(TextUtils.isEmpty(mCETCurPw.getText().toString())){
            MyToast.showShort("当前密码不可为空");
            return false;
        }
        if(TextUtils.isEmpty(mCETNewPw.getText().toString())){
            MyToast.showShort("新密码不可为空");
            return false;
        }
        if(TextUtils.isEmpty(mCETVerifyPw.getText().toString())){
            MyToast.showShort("确认新密码不可为空");
            return false;
        }


        // 重复密码确认
        String newPwd = mCETNewPw.getText().toString();
        String verifyPwd = mCETVerifyPw.getText().toString();
        if(TextUtils.isEmpty(newPwd)||!newPwd.equals(verifyPwd)){
            MyToast.showShort("两次密码输入密码不一致");
            return false;
        }
        return true;
    }

    private void save() {
        boolean boo = validate();
        if(boo){
            request();
        }
    }

    private void request() {
        if (!AppApplication.networkAvailable) {
            MyToast.showShort("没有网络");
            return;
        }

        createProgressDialog(mActivity);
        progressDialog.show();

        ChangePwServiceVisit visit = new ChangePwServiceVisit(mActivity);
        visit.setParamters(AppConfig.getInstance().getLoginedUserId(),
                MD5Tools.getInstance().getMD5(mCETCurPw.getText().toString()),
                MD5Tools.getInstance().getMD5(mCETNewPw.getText().toString()));
        //System.out.println("修改密码请求参数：" + visit.getParamter());
        visit.getResult(new NetVisitCallback<ChangePwBean,Object>(mActivity){

            @Override
            public void onSuccess(ChangePwBean data) {
                if(progressDialog!=null&&progressDialog.isShowing()){
                    progressDialog.dismiss();
                }
                if(data==null)return;
                mChangePwBean = data;
                String is = mChangePwBean.getIfModSucc();
                if(is.equals("1")){
                    Toast.makeText(mActivity, "修改成功", Toast.LENGTH_SHORT).show();
                    //清空completed
                    AppConfig.getInstance().clearCompleted();
                    mActivity.finish();
                }else{
                    Toast.makeText(mActivity, mChangePwBean.getFailReason(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(String errorEvent, String message,Object obj) {
                super.onFailure(errorEvent, message,obj);
                if(progressDialog!=null&&progressDialog.isShowing()){
                    progressDialog.dismiss();
                }
            }

        });

    }
}
